package main

import (
	"flag"
	"fmt"
	"github.com/astaxie/beego/httplib"
	"os"
	"os/signal"
)

var (
	bLoop bool
	bGet  bool
	bPost bool
	bPut  bool
	url   string
	body  string
)

func init() {
	flag.Usage()
	flag.BoolVar(&bLoop, "loop", false, "loop model")
	flag.BoolVar(&bGet, "get", false, "get:true http method is get")
	flag.BoolVar(&bPut, "put", false, "put:true http method is put")
	flag.BoolVar(&bPost, "post", false, "true:true http method is post")
	flag.StringVar(&url, "url", "", "url of request; usage: http get url=xxxx")
	flag.StringVar(&body, "body", "", "body of request; usage: http get url=xxxx body=xxxx")
}

func main() {
	flag.Parse()
	var req *httplib.BeegoHttpRequest
	if bLoop {
		go func() {
			var method string
			for {
				fmt.Println("please input mehtod,url,body:")
				fmt.Scanf("%s %s %s", &method, &url, &body)
				fmt.Printf("mehtod=%s, url=%s, body=%s\n", method, url, body)
				if method == "get" {
					req = httplib.Get(url)
				} else if method == "put" {
					req = httplib.Put(url)
				} else if method == "post" {
					req = httplib.Post(url)
				} else {
					fmt.Println("method not support ")
					continue
				}
				if body != "" {
					req.Body(body)
				}
				req.SetEnableCookie(true)
				str, err := req.Debug(true).String()
				fmt.Println(`req.Debug(true).String()):`, str, err)
			}
		}()

		//waiting for exit op
		c := make(chan os.Signal, 1)
		signal.Notify(c, os.Interrupt)
		<-c
	} else {

		if url == "" {
			fmt.Println("url isn't set or isn't empty!")
			fmt.Println("usage: http method url=xxxx body=xxxx; method=get/put/post")
			return
		}
		if !bGet && !bPut && !bPost {
			fmt.Println("http method isn't set")
			return
		}
		if bGet {
			req = httplib.Get(url)
		} else if bPut {
			req = httplib.Put(url)
		} else {
			req = httplib.Post(url)
		}
		if body != "" {
			req.Body(body)
		}
		str, err := req.Debug(true).String()
		fmt.Println(`req.Debug(true).String()):`, str, err)
	}
}
